
MsgEventHeaderCode = """
/**
 * @file Dem_SWC_Core0_CanMonitor.c
 * @brief 
 * @author zkk
 * @version 1.0
 * @date 2024-10-10
 * 
 * @copyright Copyright (c) 2024  doamincompute
 * 
 * @history:
 * Date				Version		Author		Description
 * 2024-10-10		1.0			zkk			base-line
 */

/*----------------------------------include-----------------------------------*/
#include "Rte_Dem_SWC_Core0.h"
#include "Dem_SWC_Core0_Monitor.h"
#include "ADAS_Can_Diag.h"
/*-----------------------------------macro------------------------------------*/

/*----------------------------------typedef-----------------------------------*/

"""

MsgEventFunctionCode = """
/*----------------------------------function----------------------------------*/
"""

MsgEventConfigCode = """
/*----------------------------------variable----------------------------------*/
Dem_SWC_Core0_Monitor_Cfg_t Dem_SWC_Core0_CanMonitor_Cfg[] = 
{
"""

MsgEventCode = """
};
/*------------------------------------code------------------------------------*/
#define Dem_SWC_Core0_START_SEC_CODE                   
#include "Dem_SWC_Core0_MemMap.h"
void Dem_SWC_Core0_CanMonitor_Mainfunction(void)
{
    uint8 i;
    for(i = 0;i< sizeof(Dem_SWC_Core0_CanMonitor_Cfg)/sizeof(Dem_SWC_Core0_Monitor_Cfg_t);i++)
    {
        if(Dem_SWC_Core0_CanMonitor_Cfg[i].Dem_SWC_Monitor_func() == TRUE)
        {
            Dem_SWC_Core0_CanMonitor_Cfg[i].Dem_SWC_SetEvent_func(DEM_EVENT_STATUS_PREFAILED);
        }
        else
        {
            Dem_SWC_Core0_CanMonitor_Cfg[i].Dem_SWC_SetEvent_func(DEM_EVENT_STATUS_PREPASSED);
        }
    }
}

"""

MsgEventCodeEnd = """
#define Dem_SWC_Core0_STOP_SEC_CODE  
#include "Dem_SWC_Core0_MemMap.h" 
"""
def generateSetMsgEventCode(msgName:str,eventType:str,dtcNum:str):
    global MsgEventFunctionCode
    global MsgEventConfigCode
    global MsgEventCode
    if eventType == 'CRC':
        FunctionCode = f"FUNC(Std_ReturnType, RTE_CODE) Rte_Call_Dem_SWC_Core0_PC_Monitor_Event_CRC_Error_{msgName}_SetEventStatus(VAR(Dem_EventStatusType, AUTOMATIC) EventStatus);"
        MsgEventFunctionCode += FunctionCode + '\n'
        ConfigCode = f"\t{{\tGet_{msgName}_ChecksumFaultStatus   ,           Rte_Call_Dem_SWC_Core0_PC_Monitor_Event_CRC_Error_{msgName}_SetEventStatus  ,  &Dem_SWC_dtcConfig.b_{dtcNum}  }},"
        MsgEventConfigCode += ConfigCode + '\n'
    if eventType == 'RC':
        FunctionCode = f"FUNC(Std_ReturnType, RTE_CODE) Rte_Call_Dem_SWC_Core0_PC_Monitor_Event_RC_Error_{msgName}_SetEventStatus(VAR(Dem_EventStatusType, AUTOMATIC) EventStatus);"
        MsgEventFunctionCode += FunctionCode + '\n'
        ConfigCode = f"\t{{\tGet_{msgName}_RollingCounterFaultStatus   ,           Rte_Call_Dem_SWC_Core0_PC_Monitor_Event_RC_Error_{msgName}_SetEventStatus  ,  &Dem_SWC_dtcConfig.b_{dtcNum}  }},"
        MsgEventConfigCode += ConfigCode + '\n'
    if eventType == 'Timeout':
        FunctionCode = f"FUNC(Std_ReturnType, RTE_CODE) Rte_Call_Dem_SWC_Core0_PC_Monitor_Event_Lost_COM_with_{msgName}_SetEventStatus(VAR(Dem_EventStatusType, AUTOMATIC) EventStatus);"
        MsgEventFunctionCode += FunctionCode + '\n'
        ConfigCode = f"\t{{\tGet_{msgName}_MessageTimeOut   ,           Rte_Call_Dem_SWC_Core0_PC_Monitor_Event_Lost_COM_with_{msgName}_SetEventStatus  ,  &Dem_SWC_dtcConfig.b_{dtcNum}  }},"
        MsgEventConfigCode += ConfigCode + '\n'
    if eventType == 'Signal_err':
        FunctionCode = f"FUNC(Std_ReturnType, RTE_CODE) Rte_Call_Dem_SWC_Core0_PC_Monitor_Event_Signal_{msgName}_SetEventStatus(VAR(Dem_EventStatusType, AUTOMATIC) EventStatus);"
        FunctionCode += '\n' + f"static boolean Get_{msgName}_SignalErr(void);"
        MsgEventFunctionCode += FunctionCode + '\n'
        ConfigCode = f"\t{{\tGet_{msgName}_SignalErr   ,           Rte_Call_Dem_SWC_Core0_PC_Monitor_Event_Signal_{msgName}_SetEventStatus  ,  &Dem_SWC_dtcConfig.b_{dtcNum}  }},"
        MsgEventConfigCode += ConfigCode + '\n'
        Code = f"""
static boolean Get_""{msgName}""_SignalErr(void)
{{
    boolean faultSts = FALSE;
    //""{msgName}""
    return faultSts;
}}

"""
        MsgEventCode += Code

def generateSetEventCode(eventName:str,dtcNum:str):

    msgName = None
    eventType = None
    if eventName.find("Event_CRC_Error_") != -1:
        msgName = eventName.replace("Event_CRC_Error_","")
        eventType = 'CRC'
    if eventName.find("Event_RC_Error_") != -1:
        msgName = eventName.replace("Event_RC_Error_","")
        eventType = 'RC'
    if eventName.find("Event_Lost_COM_with_") != -1:
        msgName = eventName.replace("Event_Lost_COM_with_","")
        eventType = 'Timeout'
    if eventName.find("Event_Signal_") != -1:
        msgName = eventName.replace("Event_Signal_","")
        eventType = 'Signal_err'

    if msgName != None:
        generateSetMsgEventCode(msgName,eventType,dtcNum)

def writeSetEventCode(fileName):
    global MsgEventHeaderCode
    global MsgEventFunctionCode
    global MsgEventConfigCode
    global MsgEventCode
    global MsgEventCodeEnd
    f = open(fileName,"w")
    f.write(MsgEventHeaderCode + MsgEventFunctionCode + MsgEventConfigCode + MsgEventCode + MsgEventCodeEnd)
    f.close()
